package com.example.springboot.common;

import com.example.springboot.entity.CComment;
import com.example.springboot.entity.SComment;
import lombok.Data;

import java.util.List;

@Data
public class CommentPage {
    private List list;
    private int total;
    private int originCount;

    public CommentPage scommentPage(List<SComment> list, Integer pageNum, Integer pageSize) {
        if (list.size() == 0) {
            return null;
        }
        int count = list.size(); // 记录总数
        int size = list.size();
        for(SComment comment : list) {
            size += comment.getChildren().size();
        }
        return getPage(list,size,count,pageNum,pageSize);
    }

    public CommentPage ccommentPage(List<CComment> list, Integer pageNum, Integer pageSize) {
        if (list.size() == 0) {
            return null;
        }
        int count = list.size(); // 记录总数
        int size = list.size();
        for(CComment comment : list) {
            size += comment.getChildren().size();
        }
        return getPage(list,size,count,pageNum,pageSize);
    }

    public CommentPage getPage(List list, Integer size,Integer count,Integer pageNum, Integer pageSize){
        CommentPage CommentPage = new CommentPage();
        CommentPage.setOriginCount(count);
        CommentPage.setTotal(size);
        int pageCount = 0; // 页数
        if (count % pageSize == 0) {
            pageCount = count / pageSize;
        } else {
            pageCount = count / pageSize + 1;
        }
        int fromIndex = 0; // 开始索引
        int toIndex = 0; // 结束索引
        if (!pageNum.equals(pageCount)) {
            fromIndex = (pageNum - 1) * pageSize;
            toIndex = fromIndex + pageSize;
        } else {
            fromIndex = (pageNum - 1) * pageSize;
            toIndex = count;
        }
        CommentPage.setList(list.subList(fromIndex, toIndex));
        return CommentPage;
    }
}
